<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="notice_type" label="通知类型">
				<uni-data-checkbox v-model="formData.notice_type" :localdata="activityRange"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item v-show="formData.notice_type == 0" name="system_notice_type" label="系统通知类型">
				<uni-easyinput placeholder="系统通知类型(审核结果通知、认证结果通知等)"
					v-model="formData.system_notice_type"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="notice_title" label="通知标题">
				<uni-easyinput placeholder="通知标题" v-model="formData.notice_title"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="notice_content" label="通知内容">
				<uni-easyinput placeholder="通知内容" v-model="formData.notice_content"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item v-show="formData.notice_type == 0" name="receive_user_id" label="接收用户">
				<uni-data-picker :multiple="false" v-model="formData.receive_user_id" collection="uni-id-users"
					field="nickname as text, _id as value"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item v-show="formData.notice_type == 1" name="user_read_status_array" label="已读用户">
				<uni-data-checkbox :multiple="true" v-model="formData.user_read_status_array" collection="uni-id-users"
					field="nickname as text, _id as value"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item v-show="formData.notice_type == 0" name="user_read_status" label="已读状态">
				<uni-data-checkbox v-model="formData.user_read_status" :localdata="readStatusRange"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item v-show="formData.notice_type ==1" name="activity_id" label="关联活动">
				<uni-data-picker v-model="formData.activity_id" collection="qmy-activity"
					field="name as text, _id as value"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item v-show="formData.notice_type == 1" name="activity_audit_status" label="审核">
				<uni-data-checkbox v-model="formData.activity_audit_status"
					:localdata="auditStatusRange"></uni-data-checkbox>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/qmy-notice.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'qmy-notice';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"notice_type": 0,
				"system_notice_type": "",
				"notice_title": "",
				"notice_content": "",
				"receive_user_id": "",
				"user_read_status_array": [],
				"user_read_status": 0,
				"activity_id": "",
				"activity_audit_status": 0,
			}
			return {
				formData,
				formOptions: {},
				activityRange: [{
					value: 0,
					text: "系统通知"
				}, {
					value: 1,
					text: "活动通知"
				}],
				auditStatusRange: [{
					value: 0,
					text: "审核中"
				}, {
					value: 1,
					text: "审核通过"
				}, {
					value: 2,
					text: "审核不通过"
				}],
				readStatusRange: [{
					value: 0,
					text: "未读 "
				}, {
					value: 1,
					text: "已读"
				}],
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>